/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package WebService;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;

/**
 *
 * @author Nils Ehrenström
 */

@WebService()
public class DBWebService {

    private int id = 1;

    /**
     * Web service operation
     */
    @WebMethod(operationName = "registerProduct")
    public String registerProduct(@WebParam(name = "name")
    String name, @WebParam(name = "description")
    String description, @WebParam(name = "initialPrice")
    Float initialPrice, @WebParam(name = "minimalPrice")
    Float minimalPrice, @WebParam(name = "biddingDuration")
    String biddingDuration) {

    // Exemple de valeur pour biddingDuration : P0DT10H = 10 heures

    try {
        // Conversion du String biddingDuration en Duration
        DatatypeFactory dtFactory = DatatypeFactory.newInstance();
        Duration duration = dtFactory.newDuration(biddingDuration);
        // Fichier créé dans ~/GlassFishESBv22/glassfish/domains/domain1/config/db_table_product
        PrintWriter printwriter = new PrintWriter(new FileWriter("db_table_product",true));
        // Ajouter nouvelle ligne dans fichier
        printwriter.print(id+"\t"+name+"\t"+description+"\t"+initialPrice+"\t"+minimalPrice+"\t"+duration+"\n");
        printwriter.flush();
        printwriter.close();
        id++;

    } catch (DatatypeConfigurationException ex) {
        Logger.getLogger(DBWebService.class.getName()).log(Level.SEVERE, null, ex);
        return "Error !";
    } catch (IOException ex) {
        Logger.getLogger(DBWebService.class.getName()).log(Level.SEVERE, null, ex);
        return "Error !";
    }
    return "Product Registred !";
    }

}
